<?php

class Evaluate extends Eloquent {
	protected $table  ='evaluate';
    const TYPE_QUESTION = 'question';
    const TYPE_COMPANY = 'pay_company';
    const TYPE_PRODUCT = 'product';
    const TYPE_ARTICLE = 'article';
    
    const TAG_GOOD_ATTITUDE = 'good_attitude';      // 服务态度好 
    const TAG_RESPONSIVE = 'responsive';            // 响应速度快
    const TAG_PROFESSIONAL = 'professional';        // 专业程度高
    const TAG_INITIATIVE = 'initiative';            // 主动热情
    const TAG_GOOD_DETAILS = 'good_details';        // 解答详细
    
    public static function commentExists($type, $itemId, $userId) {
    	return Evaluate::where('item_id', $itemId)->where('type', $type)->where('creator_id', $userId)->count() > 0;
    }
    
    public static function orderExists($itemId) {
    	return Order::leftjoin('order_item','order.id', '=', 'order_item.order_id')
    				->where('order.user_id', Auth::user()->id)
    				->where('order.state', Order::STATE_PAID)
    				->where('order_item.product_id', '=', $itemId)
    				->count() > 0;
    }
    
    public static function getComment($id, $type, $offset, $limit, $order_by = 'created_at', $direction = 'ASC'){
    	return Evaluate::leftjoin('user', 'evaluate.creator_id', '=', 'user.id')
    					->select(
    						'evaluate.creator_id',
    						'evaluate.score',
    						'evaluate.comment',
    						'evaluate.created_at',
    						'user.avatar',
    						'user.realname'
    						)
    					->where('evaluate.item_id', $id)
    					->where('evaluate.type', $type)
    					->take($limit)
    					->skip($offset)
    					->orderBy($order_by, $direction)
    					->get();
    }
    
    public function getUserInfo(){
    	$user = User::find($this->creator_id);
    	$userArr = [];
    	if($user){
    		$userArr['realname'] = $user->realname;
    		$userArr['avatar'] = UtilsHelper::getImage($user->avatar, 'img/dfimg.png');
    		$userArr['user_id'] = $user->id;
    	}
    	return $userArr;
    }

    public static function addEvaluation($post, $score, $comment, $tags) {
        DB::transaction(function () use ($post, $score, $comment, $tags) {
            $lawyer = $post->getAssignedLawyer();
            if ($lawyer) {
                $lawyer = $lawyer->lawyer;
                $lawyer->increment('evaluate_score', $score);
                $lawyer->increment('evaluate_count');
                $lawyer->average_evaluate_score = number_format($lawyer->evaluate_score / $lawyer->evaluate_count, 1);
                $lawyer->save();
            }
			$type = $post->type == Post::TYPE_PAY_COMPANY ? Evaluate::TYPE_COMPANY : Evaluate::TYPE_QUESTION;
            $evaluate = new Evaluate();
            $evaluate->score = $score;
            $evaluate->comment = $comment;
            $evaluate->tags = $tags;
            $evaluate->creator_id = Auth::user()->id;
            $evaluate->item_id = $post->id;
            $evaluate->type = $type;
            $evaluate->save();
        });
    }
    
    public static function getAvailableEvaluateTags() {
        return [
            self::TAG_GOOD_ATTITUDE => '服务态度好',
            self::TAG_RESPONSIVE => '回复速度快',
            self::TAG_PROFESSIONAL => '专业程度高',
            self::TAG_INITIATIVE => '主动热情',
            self::TAG_GOOD_DETAILS => '解答详细'
        ];
    }

    public static function getTagName($tag) {
        $tags = self::getAvailableEvaluateTags();
        if (isset($tags[$tag])) {
            return $tags[$tag];
        } else {
            return null;
        }
    }
}
